package com.atguigu.day26.day27morning.m1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/12/4 8:36
 * @注释
 */
public class Morning {
    public static void main(String[] args) throws Exception {
        Connection connection = DriverManager.getConnection("jdbc:mysql:///atguigu", "root", "1234");
        String sql = "select * from t_job";
        PreparedStatement pst = connection.prepareStatement(sql);
        ResultSet rs = pst.executeQuery();
        ArrayList<Job> list = new ArrayList<>();

        while (rs.next()) {
            int id = rs.getInt(1);
            String name = rs.getString(2);
            String desc = rs.getString(3);
            Job job = new Job(id, name, desc);
            list.add(job);
        }
        list.forEach(System.out::println);

        Map<Integer,Job> map = list.stream().collect(Collectors.toMap(new Function<Job, Integer>() {
            @Override
            public Integer apply(Job job) {
                return job.getId();
            }
        }, new Function<Job, Job>() {
            @Override
            public Job apply(Job job) {
                return job;
            }
        }));
       /* Map<Integer, Job> map = list.stream().collect(
                Collectors.toMap(Job::getId,d->d));*/
        rs.close();
        pst.close();
        connection.close();


        //9.遍历集合
        // list.forEach(System.out::println);
        //10.转为map

      /*  Map<Integer,Job> map = list.stream().collect(Collectors.toMap(new Function<Job, Integer>() {
            @Override
            public Integer apply(Job job) {
                return job.getId();
            }
        }, new Function<Job, Job>() {
            @Override
            public Job apply(Job job) {
                return job;
            }
        }));*/
        //Map<Integer,Job> map = list.stream().collect(Collectors.toMap(Job::getId,j->j));
    }

}
